package com.admin.server.services;

import javax.servlet.http.HttpSession;

import com.admin.server.dao.Usuarios;
import com.admin.server.util.LoadData;
import com.admin.share.model.UsuarioDTO;
import com.admin.share.services.LoginService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {

	private static final long serialVersionUID = 1L;

	@Override
	public UsuarioDTO isValidLogin(UsuarioDTO user) {
		UsuarioDTO valor = null;		
		//LoadData.load();//1
		valor = Usuarios.validarResponsable(user);
		
		if(valor != null){
			HttpSession httpSession = getThreadLocalRequest().getSession();  
		    httpSession.setAttribute("username", user.getUsuario());
		    //System.out.println(user.getUsuario());
		    //LoadData.load();//2
		    //LoadData.readMenu();
		}
		return valor;
	}

	@Override
	public UsuarioDTO getUsuarioSession() {
		HttpSession session = getThreadLocalRequest().getSession();
		if (session.getAttribute("username") != null){
			String username = (String) session.getAttribute("username");
			
	        return Usuarios.getResponsable(username);  
	    }else{
	    	return null;
	    }
	}

	@Override
	public Boolean logout() {
		Boolean retorno = true;
		try{
			HttpSession httpSession = getThreadLocalRequest().getSession(false);			
			httpSession.invalidate();
		}catch(Exception e){
			retorno = false;
		}
		
		return retorno;
	}
}
